home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Stack / Stack.h < prev    next >
C/C++ Source or Header  |  1992-06-29  |  9KB  |  215 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MJF 05/22/89 -- Initial design.
  13. // Updated: JCB 06/05/89 -- Implementation.
  14. // Updated: LGO 08/09/89 -- Inherit from Generic
  15. // Updated: MBN 08/20/89 -- Changed usage of template to reflect new syntax
  16. // Updated: MBN 08/23/89 -- Added conditional exception handling and base class
  17. // Updated: LGO 10/05/89 -- Efficiency improvements to popn
  18. // Updated: MBN 10/19/89 -- Added optional argument to set_compare method
  19. // Updated: MBN 11/01/89 -- Added constructor with user-provided storage param
  20. // Updated: LGO 12/07/89 -- re-write push and pushn, added grow method
  21. // Updated: LGO 12/07/89 -- Make compare_s default to NULL
  22. // Updated: MBN 02/22/90 -- Changed size arguments from long to unsigned long
  23. // Updated: MJF 05/31/90 -- Use memcpy in resize
  24. // Updated: MJF 05/31/90 -- Use "delete [] data"
  25. // Updated: MJF 06/30/90 -- Added base class name to constructor initializer
  26. // Updated: VDN 02/21/92 -- New lite version
  27. //
  28. // The Stack<Type>  class is publicly  derived  from  the base Stack  class and
  29. // implements a one  dimensional  vector of  a  user-specified  type.   This is
  30. // accomplished by using the parameterized type  capability of C++.   The stack
  31. // will grow dynamically as necessary with the  amount  of growth determined by
  32. // the  value of an  allocation  size slot.   Fixed   length stacks   are  also
  33. // supported by setting the value of the allocation size slot to zero.
  34. //
  35. // Each Stack<Type> object contains a private  data section that  has a slot to
  36. // hold the current size of the stack and a pointer to an allocated block large
  37. // enough to hold "size elements" of type "Type."   A slot to  thold the number
  38. // of elements currently on  the stack is also available.   The protected  data
  39. // section has   a pointer  to a  compare function  that  is used   in equality
  40. // operations. The default equality function used is the == operator.
  41. //
  42. // There  are four   constructors   for the   Stack<Type> class.   The    first
  43. // constructor takes  no arguments and creates   an  empty  Stack object of the
  44. // specified type.  The second constructor takes a required argument specifying
  45. // the initial  size  of the stack.  The  third takes a pointer   to a block of
  46. // user-defined storage    and  the number of  elements  the   stack  can hold.
  47. // Finally,  the   third constructor  takes a  single argument  consisting of a
  48. // reference to a Stack<Type> and duplicates its size and element values.
  49. //
  50. // Methods are provided to push and  pop items to  and from the  stack, examine
  51. // the   item on the   top of the stack  without  removing  it, determine if an
  52. // element is already  on the stack, report the  number of items in the  stack,
  53. // check the empty status, and clear all items from the stack.  The assignment,
  54. // output, and  equality operators are  overloaded. Finally, two methods to set
  55. // the allocation growth size and compare function are provided.
  56. //
  57.  
  58. #ifndef STACKH                    // If no definition for Stack
  59. #define STACKH                    // Define stack symbol
  60.  
  61. #ifndef BASE_STACKH                // If no definition for class
  62. #include <cool/Base_Stack.h>            // Include definition file
  63. #endif
  64.  
  65. #ifndef NEWH
  66. #if defined(DOS) || defined(M_XENIX)
  67. #include <new.hxx>                // include the new header file
  68. #else
  69. #include <new.h>                // include the new header file
  70. #endif
  71. #define NEWH
  72. #endif
  73.  
  74. template <class Type> CoolStack {
  75.   typedef Boolean (*Type##_CoolStack_Compare) (const Type&, const Type&);
  76. }
  77.  
  78. template <class Type>
  79. class CoolStack<Type> : public CoolStack {
  80. public:
  81.   CoolStack<Type> ();                // Simple constructor
  82.   CoolStack<Type> (unsigned long);        // CoolStack of initial size
  83.   CoolStack<Type> (void*, unsigned long);    // CoolStack with static storage
  84.   CoolStack<Type> (const CoolStack<Type>&);    // Copy constructor
  85.   ~CoolStack<Type> ();                // Destructor
  86.  
  87.   Boolean push (const Type&);            // Push item on top of stack
  88.   Boolean pushn (const Type&, long);        // Push n items w/initial value
  89.  
  90.   inline Type& pop ();                // Pop top item off stack
  91.   Type& popn (long);                // Remove n items, return nth
  92.   inline Type& top ();                // Return top item on stack
  93.  
  94.   inline Type& operator[] (unsigned long);    // Zero-relative (top) index 
  95.   long position (const Type&) const;        // Returns stack index of value
  96.   Boolean find (const Type&);            // Returns TRUE if found
  97.  
  98.   CoolStack<Type>& operator= (const CoolStack<Type>& s); // Assignment s = s2;
  99.   Boolean operator== (const CoolStack<Type>& s) const; // is equal
  100.   inline Boolean operator!= (const CoolStack<Type>& s) const ; // is not equal
  101.  
  102.   void resize (long);                // Resize for at least count
  103.   inline long set_length (long);        // Set number of elements
  104.   inline void set_growth_ratio (float);        // Set growth percentage
  105.   void set_compare(Type##_CoolStack_Compare = NULL); // Set compare function
  106.   inline void set_alloc_size (int);         // Set alloc size
  107.  
  108.   friend ostream& operator<< (ostream&, const CoolStack<Type>&);
  109.   inline friend ostream& operator<< (ostream&, const CoolStack<Type>*);
  110.  
  111. protected:
  112.   Type* data;                    // Pointer to allocated storage
  113.   static Type##_CoolStack_Compare compare_s;    // Pointer operator== function
  114.   Boolean grow (long min_size);            // Grow on push
  115.  
  116. private:
  117.   friend Boolean Type##_CoolStack_is_data_equal (const Type&, const Type&);
  118. };
  119.  
  120. // Type& top() -- Return the top item on this stack
  121. // Input:         None
  122. // Output:        Reference to top item on stack
  123.  
  124. template<class Type>
  125. inline Type& CoolStack<Type>::top () {
  126. #if ERROR_CHECKING
  127.   if (this->number_elements > this->size)    // If index out of range
  128.     this->top_error (#Type);            // Raise exception
  129. #endif
  130.   return this->data[this->number_elements-1];    // The top is really at the end
  131. }
  132.  
  133.  
  134. // Type& pop () -- Pop top item off this stack and return it
  135. // Input:          None
  136. // Output:         Top item of stack
  137.  
  138. template<class Type>
  139. inline Type& CoolStack<Type>::pop () {
  140. #if ERROR_CHECKING
  141.   if (this->number_elements > this->size)    // If index out of range
  142.     this->pop_error (#Type);            // Raise expception
  143. #endif
  144.   return (this->data[--this->number_elements]);    // Remove/return top element
  145. }
  146.  
  147.  
  148. // Type& operator[](long) -- Return the nth element (zero-relative from top)
  149. // Input:                    Integer n
  150. // Output:                   Reference to the nth element of stack
  151.  
  152. template<class Type>
  153. inline Type& CoolStack<Type>::operator[] (unsigned long n) {
  154. #if ERROR_CHECKING
  155.   if (unsigned(n) >= this->number_elements)    // If index out of range
  156.     this->bracket_error (#Type, n);        // Raise exception
  157. #endif
  158.   return this->data[number_elements-n-1];    // Nth element from "top"
  159. }
  160.  
  161.  
  162. // Boolean operator!= (CoolStack<Type>&) -- Compare this stack with another stack;
  163. //                                      return TRUE if they are not equal
  164. // Input:  Reference to a stack
  165. // Output: TRUE or FALSE
  166.  
  167. template<class Type>
  168. inline Boolean CoolStack<Type>::operator!= (const CoolStack<Type>& s) const {
  169.   return (!this->operator== (s));
  170. }
  171.  
  172.  
  173. // long set_length(long) -- Change number of elements in this stack
  174. // Input:                   Integer number of elements
  175. // Output:                  None
  176.  
  177. template<class Type>
  178. inline long CoolStack<Type>::set_length (long n) {
  179.   this->CoolStack::set_length (n, #Type);        // Pass size/type to base class
  180.   return this->number_elements;            // Return value
  181. }
  182.  
  183.  
  184. // void set_growth_ratio(float) -- Set growth percentage of this stack
  185. // Input:                          Float ratio
  186. // Output:                         None
  187.  
  188. template<class Type>
  189. inline void CoolStack<Type>::set_growth_ratio (float ratio) {
  190.   this->CoolStack::set_growth_ratio (ratio, #Type);    // Pass ratio/type to base
  191.  
  192.  
  193. // void set_alloc_size(int) -- Set the default allocation size
  194. // Input:                      Integer size
  195. // Output:                     None
  196.  
  197. template<class Type>
  198. inline void CoolStack<Type>::set_alloc_size (int size) {
  199.   this->CoolStack::set_alloc_size (size, #Type);
  200. }
  201.  
  202. // operator<< -- Overload the output operator for CoolStack
  203. // Input:        ostream reference, stack pointer
  204. // Output:       CoolStack data is output to ostream
  205.  
  206. template<class Type> CoolStack {
  207. inline ostream& operator<< (ostream& os, const CoolStack<Type>* s) {
  208.   return operator<<(os, *s);
  209. }
  210. }
  211.  
  212. #endif                // End #ifdef of STACKH
  213.  
  214.